<?
class MeetingLookup
{
  function getCities() {
    $html = "<option value='%'></option>";
    $db = new DB();
    $result = $db->setQuery("select distinct city from meetings where city != '' order by city");
    while ($row = $result->fetch_row()) {
        $html .= "<option>$row[0]</option>\n";
    }
    return $html;
  }

  function getZips() {
    $html = "<option value='%'></option>";
    $db = new DB();
    $result = $db->setQuery("select distinct zip from meetings where zip != '' order by zip");
    while ($row = $result->fetch_row()) {
        $html .= "<option>$row[0]</option>\n";
    }
    return $html;
  }

  function getLocations() {
    $html = "<option value='%'></option>";
    $db = new DB();
    $result = $db->setQuery("select distinct location from meetings where location != '' order by location");
    while ($row = $result->fetch_row()) {
        $html .= "<option>$row[0]</option>\n";
    }
    return $html;
  }

  function getClubhouses() {
    $html = "<option value='%'></option>";
    $db = new DB();
    $result = $db->setQuery("select distinct location from meetings 
			     where location like '%Biscayne Room%' 
			     or location like '%Clayton House%' 
			     or location like '%Clubscape%' 
			     or location like '%8111 Club%' 
			     or location like '%Freedom Club%' 
			     or location like '%Galano%' 
			     or location like '%Gwinnett Room%' 
			     or location like '%H.O.W. Place' 
			     or location like '%N.A.B.A%' 
			     or location like '%N.O.W.%' 
			     or location like '%Rebos%' 
			     or location like '%Serenity House%' 
			     or location like '%Tara%' 
			     or location like '%Triangle%' 
			     or location like '%Whole Recovery Center%' 
		  	     order by location");
    while ($row = $result->fetch_row()) {
        $html .= "<option>$row[0]</option>\n";
    }
    return $html;
  }

  function detailedSearch($location, $name, $city, $zip, $day, $time, $meridian, $codes) {
	$find_in_set = "";

	foreach ($codes as $code) {
	  if ($code != "%") {
	    $find_in_set .= " and find_in_set('$code',codes) > 0 ";
	  }
	}

    if ($location == "") { $location = "%"; }
    if ($name == "") { $name = "%"; }
    if ($zip == "") { $zip = "%"; }
    $db = new DB();
    $result = $db->setQuery("select * from meetings left join days on days.day = meetings.day left join times on times.time = meetings.time 
			     where name like '%$name%' and city like '$city' and zip like '$zip' and meetings.day like '$day' and meetings.location like '%$location%'
			     and meetings.time like '$time%$meridian' and meetings.day !='' $find_in_set order by days.sequence, times.sequence,
			     city");

    return array($result,$db->getNumRows());
  }

  function simpleSearch($city,$zip) {
    if ($city == "") { $city = "%"; }
    if ($zip == "") { $zip = "%"; }

    $db = new DB();
    $result = $db->setQuery("select * from meetings left join days on days.day = meetings.day left join times on times.time = meetings.time 
			     where city like '$city' and zip like '$zip' and  meetings.day !='' order by days.sequence, times.sequence, city");

    return array($result,$db->getNumRows());
  }

  function parseTerms($terms) {
    if (preg_match("/\d+/",$terms, $match)) {
      $zip = trim($match[0]);
    }

    if (preg_match("/[a-zA-Z\s]+/",$terms, $match)) {
      $city = trim($match[0]);
    }

    if ($city == "") { $city = "%"; }
    if ($zip == "") { $zip = "%"; }

    return(array($city, $zip));
  }

  function getAddressByRow($row) {
    $return = "";
    $db = new DB();
    $result = $db->setQuery("select concat(address1,',',city,',GA ',zip) from meetings where row = $row");
    while ($row = $result->fetch_row()) {
        $return = "$row[0]";
    }
    return $return;
  }

  function getMeetingNameByRow($row) {
    $return = "";
    $db = new DB();
    $result = $db->setQuery("select name from meetings where row = $row");
    while ($row = $result->fetch_row()) {
        $return = "$row[0]";
    }
    return $return;
  }

  function getAllByRow($row) {
    $return = array();
    $db = new DB();
    $result = $db->setQuery("select * from meetings where row = $row");
    return $result;
  }

  function auditSearch() {
    $db = new DB();
    $result = $db->setQuery("select row, city, address1 from meetings group by city, address1 order by city, address1");

    return array($result,$db->getNumRows());
  }

  
}
?>
